﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Threading;
using System.IO;

namespace AVCWizard
{
    public static class StaticExt
    {
        public const string LangFileName = "languages.xml";

        public static string AssemblyPath(string fileName)
        {
            FileInfo assembly = new FileInfo(System.Reflection.Assembly.GetExecutingAssembly().Location);
            return assembly.Directory + "\\" + fileName;
        }

        public static String ToShortTimeString(this TimeSpan time)
        {
            return String.Format("{0:00}:{1:00}:{2:00}", time.Hours, time.Minutes, time.Seconds);
        }

        public static void ExecuteThreadSafe(this Control ctrl, Action action)
        {
            if (ctrl.Dispatcher.Thread != System.Threading.Thread.CurrentThread)
            {
                ctrl.Dispatcher.Invoke(DispatcherPriority.Normal, action);
            }
            else
            {
                action.Invoke();
            }
        }

        public static void AddRangeSafe(this ItemCollection collection, object[] items)
        {
            Action insert = () => {
                foreach (var item in items)
                {
                    collection.Add(item);
                }
            };
            if (collection.Dispatcher.Thread != System.Threading.Thread.CurrentThread)
            {
                collection.Dispatcher.Invoke(DispatcherPriority.Normal, insert);
            }
            else
            {
                insert.Invoke();
            }
        }
    }
}
